/**
 * 
 */
package com.agilegroup.crm.utils;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import static com.agilegroup.crm.utils.CONSTANTS.*;
/**
 * @author hisham
 *
 */
public class AgileCalendar {
	
	public void addCalendar(String calendarName,String displayName,String email,Activity activity) {
	    Uri calUri = (Uri.parse("content://com.android.calendar/calendars"));
	    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
	    ContentValues contentValues=new ContentValues();
		if (currentapiVersion <=android.os.Build.VERSION_CODES.HONEYCOMB_MR2){
			contentValues.put(CALENDARS_NAME, calendarName);
			contentValues.put(CALENDARS_SYNC_ACCOUNT, email);
			contentValues.put(CALENDARS_SYNC_ACCOUNT_TYPE,"LOCAL");
			contentValues.put(CALENDARS_DISPLAY_NAME, displayName);
			contentValues.put(CALENDARS_VISIBLE, 0);
			contentValues.put(CALENDARS_SYNC_EVENTS, 1);
			contentValues.put(CALENDARS_OWNER_ACCOUNT, true);
			contentValues.put(CALENDARS_COLOR,Color.RED);
			contentValues.put(CALENDARS_ACCES_LEVEL, "700");
			calUri = calUri.buildUpon()
		    	    .appendQueryParameter("account_name", email)
		    	    .appendQueryParameter("account_type", "LOCAL")
		    	    .build();
		}
		else{
			contentValues.put("name", calendarName);
		    contentValues.put("account_name", email);
		    contentValues.put("account_type","LOCAL");
		    contentValues.put("calendar_displayName", displayName);
		    contentValues.put("visible", 1);
		    contentValues.put("sync_events", 1);
		    contentValues.put("ownerAccount", true);
		    contentValues.put("calendar_color",Color.RED);
		    contentValues.put("calendar_access_level", "700");
		    calUri = calUri.buildUpon()
		    	    .appendQueryParameter("caller_is_syncadapter", "true")
		    	    .appendQueryParameter("account_name", email)
		    	    .appendQueryParameter("account_type", "LOCAL")
		    	    .build();
		}
	    final Uri result = activity.getContentResolver().insert(calUri, contentValues);
	    Log.d(AgileCalendar.class.getSimpleName(), "calendar is created sucessfully");
	    Log.d(AgileCalendar.class.getSimpleName(), result.toString());
	}
	public long getUserCalendar(String email,Activity activity){
		Uri calendarURI = (Uri.parse("content://com.android.calendar/calendars"));
		String sqlWhere = null;
		int currentapiVersion = android.os.Build.VERSION.SDK_INT;
		if (currentapiVersion <=android.os.Build.VERSION_CODES.HONEYCOMB_MR2){
			sqlWhere = "_sync_account"+ " = '"+email+"'  AND  " + "_sync_account_type" + " = 'LOCAL'";
		}
		else{
			sqlWhere = "account_name"+ " = '"+email+"'  AND  " + "account_type" + " = 'LOCAL'";
		}
		Cursor cursor = activity.getContentResolver().query(calendarURI, new String[] {"_id"}, sqlWhere, null, null);
		if(cursor!=null && cursor.getCount()>0){
			if (cursor.moveToFirst()) { 
			      return cursor.getLong(0); 
			   }
		}
		  return -1;
	}
}
